<?php

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

if(!function_exists("returnJson")) {
    function returnJson($data, $message = "", $status = 200, $httpCode = false): JsonResponse {
        $result = [
            "status" => $status,
            "data" => $data,
            "message" => $message
        ];
        return response()->json($result, $httpCode ? $status : 200);
    }
}

if(!function_exists("paramCheck")) {
    function paramCheck($param) {
        $data = [];
        $result = [
            "status" => true,
            "data" => []
        ];
        $request = (new Request())->all();
        foreach ($param as $key => $item) {
            $exists = true;
            if (empty($request[$key]) && $item['require']) {
                $result = [
                    "status" => false,
                    "data" => returnJson([], "参数错误", 404, true)
                ];
                break;
            }
            switch ($item['type']) {
                case "string":
                    if(is_string($request[$key])) {
                        $data[$key] = $request[$key];
                        $exists = false;
                    }else if(!$item['require']){
                        $data[$key] = (string)$item['default'] ?: '';
                        $exists = false;
                    }
                    break;
                case "int":
                    if(is_int($request[$key])) {
                        $data[$key] = $request[$key];
                        $exists = false;
                    }else if(!$item['require']){
                        $data[$key] = (int)$item['default'] ?: 0;
                        $exists = false;
                    }
                    break;
                case "float":
                    if(is_float($request[$key])) {
                        $data[$key] = $request[$key];
                        $exists = false;
                    }else if(!$item['require']){
                        $data[$key] = (float)$item['default'] ?: 0.00;
                        $exists = false;
                    }
                    break;
                case "bool":
                    if(is_bool($request[$key])) {
                        $data[$key] = $request[$key];
                        $exists = false;
                    }else if(!$item['require']){
                        $data[$key] = (bool)$item['default'];
                        $exists = false;
                    }
                    break;
                case "json":
                    if(!empty($value = json_decode($request[$key]))) {
                        $data[$key] = $value;
                        $exists = false;
                    }else if(!$item['require']){
                        $data[$key] = $item['default'] ?: [];
                        $exists = false;
                    }
                    break;
                case "enum":
                    break;
            }
            if($exists) {
                $result = [
                    "status" => false,
                    "data" => returnJson([], "参数错误", 404, true)
                ];
                break;
            }
        }
        return $result;
    }
}

if(!function_exists("json")) {
    function json($data): bool|string {
        return json_encode($data, JSON_UNESCAPED_UNICODE );
    }
}
